/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_GEARS_
#define	_INC_AK_GEARS_

#include "ak_types.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Wait */
#define SOP_WAIT_FAST 	(0X01)
#define SOP_WAIT_NORM 	(0X02)
#define SOP_WAIT_BUSY 	(0X03)
#define SOP_WAIT_MAX 	(0X04)

/* OP will Return Ret, if Ret != 0, Just Wait */
#define SOP_OP_WAIT(vOP, vRet, vIter, vRetry, vWait) \
	do{\
		(vIter) = 0; (vRet) = -1; \
		do{\
			vOP; \
			if(((vRet) != 0) && ((vIter) < (vRetry))){\
				(vIter) += 1;	SYS_SLEEP(vWait);\
			}else{\
				break;\
			}\
		}while(1);\
	}while(0)

/* App Define Marco */
#define S_APP_MAX_NAME 		31
#define S_APP_MAX_PATH 		127

extern	int 	SStrJ_Avail(char * name);
extern	int 	SStrJ_Path(char * path, char * curr);
extern	int 	SStrJ_S2Int(char * intStr);

#define S_APP_MAX_PARAM 	10
extern	int 	SStrJ_Param(char * pParam, 	char ** aParam);

/* -----------------------------------------------------------------------------
** 	Ak-Lib's Hash Function Interface: 
*/
#define 	AK_YHASH(vStr, vLen) 		_Ak_YHash((const void *)(vStr), (unsigned int)(vLen))
#define 	AK_YHASH32(iStr, ilen) 	_Ak_YHash32((const unsigned int *)(iStr), (unsigned int)(ilen))
#define 	AK_DJBHASH(vStr, vLen) 	_Ak_DJBHash((const unsigned char *)(vStr), (unsigned int)(vLen))

extern	unsigned int 	_Ak_YHash(const void *key, unsigned int length);
extern	unsigned int 	_Ak_YHash32(const unsigned int *key, unsigned int length);

/* Dan Bernstein's string hash function, Times33 */
#if 0
static 	__inline 	unsigned int 	Ak_DJBHashSimple(const unsigned char *s)
{
	unsigned char c;
	unsigned int  h = 5381;
	for(c = *s; c != '\0'; c = *(++s)){
		h = (h<<5) + h + c;
	}
    return h;
}
#endif

static 	__inline 	unsigned int 	_Ak_DJBHash (const unsigned char *s, unsigned int length)
{
	unsigned int  i;
	unsigned int  h = 5381;
	i = length >> 2;
	while(i--){
		h = (h<<5) + h + (*s++);
		h = (h<<5) + h + (*s++);
		h = (h<<5) + h + (*s++);
		h = (h<<5) + h + (*s++);
	}
	i = length & 0X03;
	while(i--){
		h = (h<<5) + h + (*s++);
	}
    return h;
}


/*	---------------------------------------------------------------------------
**	Application Medium-Resolution Timing Measurement.
*/
#define 	_AK_MTIMING_HI
typedef struct _t_mTiming{
	/* Linux */
#if 	_TARGET_OS == _OS_GNU
	struct timeval	tBeg;
	struct timeval 	tEnd;
	/* Windows */
#elif 	_TARGET_OS == _OS_WIN
	#ifdef _AK_MTIMING_HI
	/* This is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). */
	SYSTEMTIME		tBeg;
	SYSTEMTIME		tEnd;
	LARGE_INTEGER 	lBeg;
	LARGE_INTEGER	lEnd;
	#else
	/* It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should
    	** Convert the SYSTEMTIME structure to a FILETIME structure.
    	** Copy the resulting FILETIME structure to a ULARGE_INTEGER structure.
    	** Use normal 64-bit arithmetic on the ULARGE_INTEGER value.  GetSystemTimeAsFileTime
    	**
    	** Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value,
    	** because it can cause alignment faults on 64-bit Windows.
    	*/
	SYSTEMTIME		tBeg;
	SYSTEMTIME		tEnd;
	FILETIME  		fBeg;
	FILETIME  		fEnd;
	ULARGE_INTEGER	lBeg;
	ULARGE_INTEGER	lEnd;
	#endif
#endif
	/* Count in Ms */
	long long		diff;
}_t_mTiming;

/* Linux */
#if 	_TARGET_OS == _OS_GNU
#define STIMING_BEG(vTiming) 	\
do{\
	gettimeofday(&((vTiming).tBeg), NULL);\
}while(0)

#define STIMING_END(vTiming) 	\
do{\
	gettimeofday(&((vTiming).tEnd), NULL);\
	(vTiming).diff = 0;\
	if((vTiming).tEnd.tv_sec > (vTiming).tBeg.tv_sec){\
		(vTiming).diff = (1000000) * ((vTiming).tEnd.tv_sec - (vTiming).tBeg.tv_sec);\
	}\
	(vTiming).diff = ((vTiming).diff + (vTiming).tEnd.tv_usec - (vTiming).tBeg.tv_usec);\
}while(0)

#define STIMING_PRINT(vTiming, vStr) \
do{\
	printf("%s >> [%-4d:%-3d:%-3d] - [%-3d:%-3d:%-3d] (%lld)\n",\
		vStr,\
		(int)((vTiming).tBeg.tv_sec%1000), (int)((vTiming).tBeg.tv_usec/1000), (int)((vTiming).tBeg.tv_usec%1000),\
		(int)((vTiming).tEnd.tv_sec%1000), (int)((vTiming).tEnd.tv_usec/1000), (int)((vTiming).tEnd.tv_usec%1000),\
		(vTiming).diff);\
}while(0)

/* Windows */
#elif 	_TARGET_OS == _OS_WIN
#ifdef _AK_MTIMING_HI
#define STIMING_BEG(vTiming) 	\
do{\
	NtQuerySystemTime (&((vTiming).lBeg));\
	GetSystemTime(&((vTiming).tBeg));\
}while(0)

#define STIMING_END(vTiming) 	\
do{\
	GetSystemTime(&((vTiming).tEnd));\
	NtQuerySystemTime (&((vTiming).lEnd));\
	(vTiming).diff = ((vTiming).lEnd.QuadPart - (vTiming).lBeg.QuadPart)/10;\
}while(0)

#define STIMING_PRINT(vTiming, vStr) \
do{\
	printf("%s >> [%-2d:%-2d:%-3d] - [%-2d:%-2d:%-3d] (%lld)\n",\
		vStr,\
		(int)((vTiming).tBeg.wMinute), (int)((vTiming).tBeg.wSecond), (int)((vTiming).tBeg.wMilliseconds),\
		(int)((vTiming).tEnd.wMinute), (int)((vTiming).tEnd.wSecond), (int)((vTiming).tEnd.wMilliseconds),\
		(vTiming).diff);\
}while(0)

#else
#define STIMING_BEG(vTiming) 	\
do{\
	GetSystemTime(&((vTiming).tBeg));\
	SystemTimeToFileTime(&((vTiming).tBeg), &((vTiming).fBeg));\
}while(0)

#define STIMING_END(vTiming) 	\
do{\
	GetSystemTime(&((vTiming).tEnd));\
	SystemTimeToFileTime(&((vTiming).tEnd), &((vTiming).fEnd));\
	(vTiming).lBeg.u.LowPart  = (vTiming).fBeg.dwLowDateTime;\
	(vTiming).lBeg.u.HighPart = (vTiming).fBeg.dwHighDateTime;\
	(vTiming).lEnd.u.LowPart  = (vTiming).fEnd.dwLowDateTime;\
	(vTiming).lEnd.u.HighPart = (vTiming).fEnd.dwHighDateTime;\
	(vTiming).diff = (vTiming).lEnd.QuadPart - (vTiming).lBeg.QuadPart;\
}while(0)

#define STIMING_PRINT(vTiming, vStr) \
do{\
	printf("%s >> [%-2d:%-2d:%-3d] - [%-2d:%-2d:%-3d] (%lld)\n",\
		vStr,\
		(int)((vTiming).tBeg.wMinute), (int)((vTiming).tBeg.wSecond), (int)((vTiming).tBeg.wMilliseconds),\
		(int)((vTiming).tEnd.wMinute), (int)((vTiming).tEnd.wSecond), (int)((vTiming).tEnd.wMilliseconds),\
		(vTiming).diff);\
}while(0)
#endif

#endif

/* Linux */
#if 	_TARGET_OS == _OS_GNU
/* Only 1 Instance for Dev Agent */
extern	int 	Sys_ChkInst(char * shmName);
/* Windows */
#elif 	_TARGET_OS == _OS_WIN
#endif


/* Prime Number:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 
307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 
401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 
503 509 521 523 541 547 557 563 569 571 577 587 593 599 
601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 
701 709 719 727 733 739 743 751 757 761 769 773 787 797 
809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 
907 911 919 929 937 941 947 953 967 971 977 983 991 997
1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097
1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 
1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 
1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 
1409 1423 1427 1429 1433 1439 1447 14511453 1459 1471 1481 1483 1487 1489 1493 1499
1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 
1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 
1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 
1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 
1901 1907 1913 1931 1933 1949 1951 1973 1979 19871993 1997 1999 
*/

#ifdef __cplusplus
}
#endif

#endif


